package com.xiaohai.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xiaohai.common.GetContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * ClassName:MyMetaObjectHandler
 * Package:IntelliJ IDEA
 * Description: 自动填充功能
 * Author 何诗海
 *
 * @Create 2024/1/16 9:00
 * @Version 1.0
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Long id = GetContext.getThread();
        metaObject.setValue("createTime", new Date());
        metaObject.setValue("createUser", id);
        metaObject.setValue("updateTime", new Date());
        metaObject.setValue("updateUser", id);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("自动填充update：{}", metaObject);
        Long id = GetContext.getThread();
        metaObject.setValue("updateTime", new Date());
        metaObject.setValue("updateUser", id);
    }
}
